<script setup>
import { ref, reactive } from 'vue'
import CodeBlock from '@components/CodeBlock.vue';

// 用于演示CSS变量和动态样式
const theme = reactive({
  primaryColor: '#667eea',
  secondaryColor: '#764ba2',
  borderRadius: '8px',
  padding: '16px'
});

// 切换主题颜色
const toggleTheme = () => {
  if (theme.primaryColor === '#667eea') {
    theme.primaryColor = '#ff6b6b';
    theme.secondaryColor = '#4ecdc4';
  } else {
    theme.primaryColor = '#667eea';
    theme.secondaryColor = '#764ba2';
  }
};

// 切换组件状态
const isActive = ref(false);
const toggleActive = () => {
  isActive.value = !isActive.value;
};

// 子组件数据
const childMessage = ref('我是子组件的消息');
</script>

<template>
  <div class="css-features-demo" :style="themeVars">
    <h2 class="demo-title">🎨 Vue 3 CSS 特性演示</h2>
    
    <div class="demo-controls">
      <button class="theme-toggle-btn" @click="toggleTheme">切换主题颜色</button>
      <button class="state-toggle-btn" @click="toggleActive">切换激活状态</button>
    </div>
    
    <div class="demo-grid">
      <!-- CSS 变量演示 -->
      <div class="demo-card">
        <h3 class="card-title">CSS 变量</h3>
        <div class="demo-content">
          <p>Vue 3 支持在组件中使用 CSS 变量，并可以通过内联样式动态更新它们。</p>
          <div class="css-vars-example">
            <div class="color-box primary"></div>
            <div class="color-box secondary"></div>
          </div>
          <div class="code-snippet">
            <CodeBlock :code="themeVarsCode" language="css"></CodeBlock>
          </div>
        </div>
      </div>
      
      <!-- 深度选择器演示 -->
      <div class="demo-card">
        <h3 class="card-title">深度选择器</h3>
        <div class="demo-content">
          <p>使用 :deep() 伪类选择器可以穿透 scoped 样式限制。</p>
          <div class="deep-selector-example">
            <div class="outer-container">
              <div class="inner-element">内层元素</div>
            </div>
          </div>
          <div class="code-snippet">
            <CodeBlock :code="deepSelectorCode" language="css"></CodeBlock>
          </div>
        </div>
      </div>
      
      <!-- 动态 CSS 类演示 -->
      <div class="demo-card">
        <h3 class="card-title">动态 CSS 类</h3>
        <div class="demo-content">
          <p>基于组件状态动态应用 CSS 类。</p>
          <div class="dynamic-class-example" :class="{ active: isActive }">
            这个元素的类会根据状态变化: {{ isActive ? '激活' : '非激活' }}
          </div>
          <div class="code-snippet">
            <CodeBlock :code="dynamicClassCode" language="css"></CodeBlock>
          </div>
        </div>
      </div>
      
      <!-- 插槽选择器演示 -->
      <div class="demo-card">
        <h3 class="card-title">插槽选择器</h3>
        <div class="demo-content">
          <p>使用 :slotted() 伪类选择器可以选择插槽内容。</p>
          <div class="slotted-example">
            <div class="slot-container">
              <slot>
                <p class="default-slot-content">默认插槽内容</p>
              </slot>
            </div>
          </div>
          <div class="code-snippet">
            <CodeBlock :code="slottedSelectorCode" language="css"></CodeBlock>
          </div>
        </div>
      </div>
      
      <!-- 全局选择器演示 -->
      <div class="demo-card">
        <h3 class="card-title">全局选择器</h3>
        <div class="demo-content">
          <p>使用 :global() 伪类选择器可以定义全局样式。</p>
          <div class="global-selector-example">
            <button class="global-button">全局样式按钮</button>
          </div>
          <div class="code-snippet">
            <CodeBlock :code="globalSelectorCode" language="css"></CodeBlock>
          </div>
        </div>
      </div>
      
      <!-- 组件样式隔离演示 -->
      <div class="demo-card">
        <h3 class="card-title">组件样式隔离</h3>
        <div class="demo-content">
          <p>Vue 3 的样式隔离确保组件样式不会泄漏到外部。</p>
          <div class="css-isolation-example">
            <p class="isolated-text">这个文本使用了组件内隔离的样式</p>
          </div>
          <div class="code-snippet">
            <CodeBlock :code="isolationCode" language="css"></CodeBlock>
          </div>
        </div>
      </div>
    </div>
  </div>
</template>

<script>
export default {
  data() {
    return {
      // 用于代码展示的字符串
      themeVarsCode: `// 组件中定义CSS变量
:style="{
  '--primary-color': theme.primaryColor,
  '--secondary-color': theme.secondaryColor,
  '--border-radius': theme.borderRadius,
  '--padding': theme.padding
}"`,
      deepSelectorCode: `// 深度选择器用法
.outer-container :deep(.inner-element) {
  color: var(--primary-color);
  font-weight: bold;
}`,
      dynamicClassCode: `// 动态CSS类
<div :class="{ active: isActive }">
  状态: {{ isActive ? '激活' : '非激活' }}
</div>`,
      slottedSelectorCode: `// 插槽选择器
.slot-container :slotted(.default-slot-content) {
  color: var(--secondary-color);
}`,
      globalSelectorCode: `// 全局选择器
:global(.global-button) {
  background-color: var(--primary-color);
  color: white;
  border: none;
  padding: 8px 16px;
  border-radius: 4px;
}`,
      isolationCode: `// 组件样式隔离 (默认启用)
<style scoped>
.isolated-text {
  color: var(--secondary-color);
  font-style: italic;
}
</style>`
    }
  },
  computed: {
    themeVars() {
      return {
        '--primary-color': this.theme.primaryColor,
        '--secondary-color': this.theme.secondaryColor,
        '--border-radius': this.theme.borderRadius,
        '--padding': this.theme.padding
      }
    }
  }
}
</script>

<style scoped>
.css-features-demo {
  padding: 20px;
}

.demo-title {
  text-align: center;
  margin-bottom: 30px;
  background: linear-gradient(135deg, var(--primary-color) 0%, var(--secondary-color) 100%);
  -webkit-background-clip: text;
  -webkit-text-fill-color: transparent;
  background-clip: text;
}

.demo-controls {
  display: flex;
  gap: 15px;
  justify-content: center;
  margin-bottom: 30px;
}

.theme-toggle-btn, .state-toggle-btn {
  padding: 10px 20px;
  border: none;
  border-radius: var(--border-radius);
  background-color: var(--primary-color);
  color: white;
  cursor: pointer;
  transition: background-color 0.3s;
}

.theme-toggle-btn:hover, .state-toggle-btn:hover {
  opacity: 0.9;
}

.state-toggle-btn {
  background-color: var(--secondary-color);
}

.demo-grid {
  display: grid;
  grid-template-columns: repeat(auto-fit, minmax(300px, 1fr));
  gap: 20px;
}

.demo-card {
  background: white;
  border-radius: var(--border-radius);
  box-shadow: 0 2px 10px rgba(0, 0, 0, 0.1);
  padding: var(--padding);
}

.card-title {
  color: var(--primary-color);
  margin-bottom: 15px;
}

.demo-content p {
  margin-bottom: 15px;
  color: #555;
}

/* CSS 变量演示样式 */
.css-vars-example {
  display: flex;
  gap: 10px;
  margin-bottom: 15px;
}

.color-box {
  width: 50px;
  height: 50px;
  border-radius: 5px;
}

.primary {
  background-color: var(--primary-color);
}

.secondary {
  background-color: var(--secondary-color);
}

/* 深度选择器演示样式 */
.deep-selector-example .outer-container {
  padding: 15px;
  background-color: #f0f0f0;
  border-radius: 5px;
}

.deep-selector-example .outer-container :deep(.inner-element) {
  color: var(--primary-color);
  font-weight: bold;
}

/* 动态CSS类演示样式 */
.dynamic-class-example {
  padding: 15px;
  background-color: #f0f0f0;
  border-radius: 5px;
  transition: all 0.3s;
}

.dynamic-class-example.active {
  background-color: var(--primary-color);
  color: white;
}

/* 插槽选择器演示样式 */
.slotted-example .slot-container {
  padding: 15px;
  background-color: #f0f0f0;
  border-radius: 5px;
}

.slotted-example .slot-container :slotted(.default-slot-content) {
  color: var(--secondary-color);
  font-weight: bold;
}

/* 全局选择器演示样式 */
:global(.global-button) {
  background-color: var(--primary-color);
  color: white;
  border: none;
  padding: 8px 16px;
  border-radius: 4px;
  cursor: pointer;
}

/* 组件样式隔离演示样式 */
.isolated-text {
  color: var(--secondary-color);
  font-style: italic;
}

/* 代码片段样式 */
.code-snippet {
  background-color: #f5f5f5;
  border-radius: 5px;
  padding: 10px;
  overflow-x: auto;
}

.code-snippet pre {
  margin: 0;
  font-family: 'Courier New', Courier, monospace;
  font-size: 12px;
  color: #333;
}

/* 响应式调整 */
@media (max-width: 768px) {
  .demo-controls {
    flex-direction: column;
    align-items: center;
  }
  
  .demo-grid {
    grid-template-columns: 1fr;
  }
}
</style>